// start: 0 服务商订单 1发起方订单
// orderSource: 1中医 0宜健
// status: 0待服务 1未付款 2进行中 3已完成 4已取消 5交易关闭 6待采购
// checkStatus: 0待核销 1已核销
// mailingInfo.mailingStatus 邮寄状态: 0无 1待发货 2已发货 3已收货
// mailingInfo.logisticType 物流类型: 0快递 1配送点自取 2无需物流 3同城配送
// mailingInfo.mailingWay  0配送点自取 2邮寄给诊所 3邮寄给患者
// parescriptOrder.prescriptionType 处方类型： 1贴敷处方

// 是否需要核销
export function whetherSettle(order) {
  // 服务商订单 && 待服务 && 不等于已核销
  return order.start === "0" && order.status == 0 && order.checkStatus !== 1;
}

// 是否需要收款
export function nonPayment(order) {
  // 服务商订单 && 未付款
  return order.start === "0" && order.status == 1;
}

// 是否显示结算汇总
export function showSettlement(order) {
  // 已完成
  return order.status == 3;
}

// 是否需要发货
export function whetherConsignment(order) {
  // 服务商订单 && (待服务 || 进行中)
  if (order.start === "0" && (order.status == 0 || order.status == 2)) {

    if (!order.mailingInfo) return false;
    const { mailingStatus, logisticType } = order.mailingInfo;
    // 邮寄状态：待发货 && (物流类型： 快递 || 同城配送)
    if (mailingStatus === 1 && (logisticType === 0 || logisticType === 3)) {
      return true;
    } else {
      return false;
    }
  } else {
    return false;
  }
}

// 是否显示取消订单
export function showCancelOrder(order) {
  // 服务商订单 && (待服务 || 进行中)
  return order.start === "0" && (order.status == 0 || order.status == 2);
}

// 取药状态展示
export function getMedicationStatusTitle(order) {
  const { medicationStatus } = order;
  if (medicationStatus === 0) {
    return {
      title: "未取药",
      color: "#f56c6c",
    };
  } else if (medicationStatus === 1) {
    return {
      title: "部分取药",
      color: "#f56c6c",
    };
  } else if (medicationStatus === 2) {
    return {
      title: "全部取药",
      color: "#00ff00",
    };
  }else{
    return false
  }
}

// 物流信息展示
export function getLogisticInfoTitle(order){
  if (!order.mailingInfo) return "";
  const { logisticType, mailingWay } = order.mailingInfo;
  if(logisticType === 0 && mailingWay === 3){
    return "快递（邮寄给患者）"
  }else if(logisticType === 0 && mailingWay === 2){
    return "快递（邮寄给诊所）"
  }else if((logisticType === 0 && mailingWay === 0) || logisticType === 1){
    return "配送点自取"
  }else if(logisticType === 2){
    return "无需物流"
  }else if(logisticType === 3){
    return "同城配送"
  }
}

// 支付方式展示
export function getPlayTypeTitle(order){
  const { status, playCreateTime, payWay } = order;
  // 未付款 || 交易关闭 || 不存在支付时间
  if(status == 1 || status == 5 || !playCreateTime) return "-";
  if(payWay === 1 || payWay === 3){
    return "线上支付"
  }else if(payWay === 2 || payWay === 4){
    return "线下支付"
  }else{
    return "-"
  }
}

// 获取收货地址
export function getShippingAddress(order) {
  const { parescriptOrder, mailingInfo } = order;
  if(parescriptOrder && parescriptOrder.drugMailInfo){
    const { recipientName, recipientPhone, province, mailingAddr } = parescriptOrder.drugMailInfo;
    return `${ recipientName } ${ recipientPhone } ${ province }${ mailingAddr }`
  }else if(mailingInfo){
    const { recipientName, recipientPhone, mailingAddr } = mailingInfo;
    return `${ recipientName } ${ recipientPhone } ${ mailingAddr }`
  }
}

// 获取订单金额
export function order_dingdanjinge(order){
  const { start, orderSource } = order;
  if(orderSource === 1){ // 中医订单
    if(start === "0"){ // 服务商订单
      return order.expertRepayCost // 专家代付金额
    }else{
      return order.cost
    }
  }else{ // 宜健订单
    return order.costOriginal
  }
}

// 获取实付金额
export function order_shifujinge(order){
  const { orderSource, status, parescriptOrder } = order;
  // 未付款 || 交易关闭 || 待采购（已收款但是未付款）
  if(status == 1 || status == 5 || status == 6){
    return ""
  }else{
    if(orderSource === 1 && !!parescriptOrder){ // 中医订单 && 存在处方
      const { prescriptionType } = parescriptOrder;
      if(prescriptionType == 1){ // 处方类型： 1贴敷处方
        return order.collectCost // 收款金额
      }else{// 中药处方等
        return order.expertRepayCost // 专家代付金额
      }
    }else{ // 宜健订单
      return order.cost
    }
  }
}

// 获取退款金额
export function order_tuikuanjinge(order){
  const { status, refundcost } = order;
  // 已取消 && 存在退款金额
  if(status == 4 && refundcost){
    const cost = order_shifujinge(order);
    return cost == refundcost ? "已全额退款" : `退款金额 ￥${ refundcost }`
  }
  return ""
}

// 导出相关方法
import { getCheckOrderList } from "@/api/service";

export function getDownloadTitle(type) {
  return type === "detail" ? `服务项目明细.xls` : `服务订单列表.xls`
}

export function getTableFields(type) {
  const common = {
    订单编号: "orderNo",
    订单状态: "statusTitle", //
    开单时间: "createTime",
    开单申请人: "sponsor",
    申请人所属机构: "companyName",
    服务所属机构: "checkAgencyName",
    订单归属渠道: "channelName",
    订单归属业务员: "businessUserName",
  }

  const list = {
    订单类型: "orderSourceTitle",
    处方类型: "parescriptOrder_prescriptionType",
    物流方式: "mailingInfo_logisticInfoTitle",
    消费人姓名: "parescriptOrder_patientName",
    消费人性别: "parescriptOrder_patientSex",
    消费人年龄: "parescriptOrder_patientAge",
    消费人电话: "parescriptOrder_patientPhone",
    订单金额: "dingdanjinge",
    实付金额: "shifujinge",
    退款金额: "tuikuanjinge",
  }

  // checkItem为维度产生明细数据
  const detail = {
    服务归属: "sheetList_sheetType", // 取字典service_items
    收费项目类型: "sheetList_dictLabel",
    项目名称: "xiangmumingcheng", // checkItem.itemName
    项目单位: "xiangmudanwei",
    项目数量: "xiangmushuliang",
  }

  return type === "detail" ? Object.assign(common, detail) : Object.assign(common, list)
}

export async function recombineData(type, that) {
  if(that.total > 1000){
    this.$message.error('检测数据量超过1000条，请缩小查询范围后导出');
    return
  }

  const query = {...that.queryParams, pageNum: 1, pageSize: that.total};

  const message = that.$message({
    message: '正在处理导出数据，请勿刷新或关闭网页，否则会丢失数据。',
    type: 'warning',
    duration: 0,
  });


  const response = await getCheckOrderList(query);
  const { rows } = response;
  let exportList = [];

  rows.forEach(row=>{
    row.statusTitle = that.dict.type.order_status.find(dict=> dict.value === row.status).label;
    row.orderSourceTitle = row.orderSource === 1 ? "中医订单" : "宜健订单";
    row.parescriptOrder_prescriptionType = row.parescriptOrder ? that.dict.type.tcm_prescript_type.find(dict=> dict.value === row.parescriptOrder.prescriptionType).label : '';
    row.mailingInfo_logisticInfoTitle = getLogisticInfoTitle(row);
    row.parescriptOrder_patientName = row.parescriptOrder && row.parescriptOrder.patientName ? row.parescriptOrder.patientName : (row.username || '');
    row.parescriptOrder_patientSex = row.parescriptOrder && row.parescriptOrder.patientName ? (row.parescriptOrder.patientSex === 1 ? '女' : '男') : '';
    row.parescriptOrder_patientAge = row.parescriptOrder && row.parescriptOrder.patientName ? row.parescriptOrder.patientAge : '';
    row.parescriptOrder_patientPhone = row.parescriptOrder && row.parescriptOrder.patientName ? row.parescriptOrder.patientPhone : row.userphone;
    row.dingdanjinge = order_dingdanjinge(row);
    row.shifujinge = order_shifujinge(row) ? order_shifujinge(row) : '';
    row.tuikuanjinge = order_shifujinge(row) ? order_tuikuanjinge(row) : '';

    if(type === 'detail'){
      row.sheetList.forEach(item=> {
        const _typeItem = that.dict.type.service_items.find(dict=> dict.value == item.serviceItems);
        item.checkItem.forEach(checkItem=>{
          exportList.push({
            ...row,
            sheetList_sheetType: _typeItem ? _typeItem.label : '其他类',
            sheetList_dictLabel: item.dictLabel,
            xiangmumingcheng: checkItem.itemName,
            xiangmudanwei: checkItem.unit,
            xiangmushuliang: item.prescriptNum ? (item.prescriptNum * checkItem.specsNum) : (item.itemNum * checkItem.specsNum)
          })
        })
      })
    }
  })

  message.close();
  return type === 'detail' ? exportList : rows
}
